@ECHO OFF
REM 设置字符编码
chcp 65001 > nul

setlocal
set noticeMsg=请输入要添加到环境变量Path的目录路径
set newPath=
set targetPath=%~1
call:resolvePath

set act=%~2
if "%act%"=="" (
    set act=install
)
set log=%~3
if "%log%"=="" (
    set log=enable
)

REM echo 获取用户环境变量
REM wmic ENVIRONMENT where "name='path' and username='%computername%\\%username%'" get VariableValue | find ";"
REM echo=
REM echo 获取系统环境变量
REM wmic ENVIRONMENT where "name='path' and username='<system>'" get VariableValue | find ";"
REM echo=
REM rem echo 设置环境变量 %~n0_home=%~dp0
REM echo %~dp$PATH:0
REM echo %path%
REM echo=

REM 读取环境变量
set varPath=
for /f "delims=" %%i in ('wmic ENVIRONMENT where "name='path' and username='%computername%\\%username%'" get VariableValue ^| find ";"') do set varPath=%%i
rem echo userPath=%varPath%
REM echo targetPath=%targetPath%
if "%act%"=="install" (
    call:addToPath "%targetPath%"
) else (
    call:delFromPath "%targetPath%"
)
endlocal
pause
goto:eof
REM 逻辑结束

REM 获取设置的路径
:resolvePath
REM echo targetPath=%targetPath% 
if "%targetPath%"=="" (
    set /p targetPath=%noticeMsg%
)

if not exist "%targetPath%" (
	echo %targetPath% 不存在，请输入正确目录路径：
    set /p targetPath=%noticeMsg%
	goto:resolvePath
)

if "%targetPath:~-1%"=="\" (
    REM call:getLength "%targetPath%"
    REM echo length_called=%length%
    REM echo targetPath_called=%targetPath%
) else (
    if not exist "%targetPath%\" (
        echo %targetPath% 不是目录，请输入正确目录路径：
        set /p targetPath=%noticeMsg%
        REM set /p targetPath=please enter dir path
        goto:resolvePath
    )
)
exit /b

REM 添加到环境变量
:addToPath
set targetPath=%~1
echo %varPath% | findstr /C:%targetPath% > nul
rem echo errorlevel=%errorlevel%
if %errorlevel%==1 (
    rem echo new path="%~dp0;%varPath%;"
    if "%act%"=="install" (
        wmic ENVIRONMENT where "name='path' and username='%computername%\\%username%'" set VariableValue="%targetPath%;%varPath%;" > nul
        if "%log%"=="enable" (
            echo ==============================================================================================================
            echo=
            echo   成功注册%targetPath%到环境变量PATH
            echo=
            echo ==============================================================================================================
        )
    )
) else (
    if "%log%"=="enable" (
        echo ==============================================================================================================
        echo=
        echo   PATH已经包含%targetPath%，不需要注册
        echo=
        echo ==============================================================================================================
    )
)
exit /b

:delFromPath
set targetPath=%~1
echo %varPath% | findstr /C:%targetPath% > nul
rem echo errorlevel=%errorlevel%
if %errorlevel%==0 (
    REM echo %~dp0del_from_path.bat %varPath% %targetPath%
    REM echo varPath="%varPath%"
    setlocal EnableDelayedExpansion
    set queryPath=!varPath:%%=%%%%!
    set delPath=!targetPath!
    call:loop
    endlocal
    if "%log%"=="enable" (
        echo ==============================================================================================================
        echo=
        echo   成功从环境变量PATH移除%targetPath%
        echo=
        echo ==============================================================================================================
    )
)
exit /b 0

:loop

if "%queryPath%"=="" (exit /b 0)
for /f "tokens=1* delims=;" %%a in ("%queryPath%") do (
    call:resetPath "%%a" "%delPath%"
    setlocal EnableDelayedExpansion
    set queryPath=%%b
    if "!queryPath!"=="" (
        REM echo end loop
        wmic ENVIRONMENT where "name='path' and username='%computername%\\%username%'" set VariableValue="%newPath%" > nul
    ) else (
        REM echo "!queryPath!" "!queryPath!"
        goto :loop
    )
)
exit /b 0

:resetPath
REM echo -------------------------------------------------------------------
REM echo resetPath
REM echo=
REM echo arug0="%~0"
REM echo arug1="%~1"
REM echo arug2="%~2"

set currentPath=%~1
set delPath=%~2

REM echo currentPath=%currentPath%
REM echo delPath=%delPath%

if "%currentPath%"=="%delPath%" (
    REM echo "%currentPath%"=="%delPath%" = true
) else (
    REM echo 不同于
    if "%newPath%"=="" (
        REM echo set newPath=%currentPath%
        set newPath=%currentPath%
    ) else (
        REM echo set newPath=%newPath%;%currentPath%
        set newPath=%newPath%;%currentPath%
    )
)
REM echo newPath=%newPath%
exit /b 0

REM :getLength
REM setlocal enabledelayedexpansion
REM set str=%~1
REM for /l %%a in (0,1,8191) do (
    REM Set "each=!str:~%%a,1!"
    REM if not defined each (
        REM set /a length=%%a-1
        REM echo length:!length!
        REM echo length:!!length!!
        REM Set "targetPath2=!targetPath:~0,%%length%%!"
        set targetPath=!targetPath:~0,%%length%%!
        REM echo targetPath2=!targetPath2!
        REM endlocal
        REM exit /b 0
    REM )
    if not defined each & endlocal & set /a length=%%a-1 & echo length:%%length%% & Set "targetPath=!targetPath:~0,%%length%%!" & echo %targetPath% & exit /b 0
REM )
REM endlocal
REM exit /b 1